<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item>
          <el-input placeholder="用户UID" v-model="form.uid"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input placeholder="商户ID" v-model="form.merechant_id"></el-input>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="form.timeData"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="截止日期"
            :default-time="pickerDefaultTime"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" @click="onAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="id" align="center" width="60" />
          <el-table-column prop="uid" label="用户UID" align="center" />
          <el-table-column
            prop="merchant_id"
            label="商户id"
            align="center"
            width="110"
          />
          <el-table-column
            prop="merchant_name"
            label="商户名称"
            align="center"
            width="120"
          />
          <el-table-column
            prop="account_type_name"
            label="商户类型"
            align="center"
          ></el-table-column>
          <el-table-column label="基本信息" align="left" width="300">
            <template #default="scope">
              <p>公司名称：{{ scope.row.company_name }}</p>
              <p>公司别名：{{ scope.row.company_alias }}</p>
              <p>经营范围：{{ scope.row.company_business }}</p>
              <p>个人姓名：{{ scope.row.real_name }}</p>
              <p>证件号码：{{ scope.row.real_number }}</p>
              <p>联系方式：{{ scope.row.contact }}</p>
              <p>网站url：{{ scope.row.website }}</p>
              <p>地址信息：{{ scope.row.address }}</p>
            </template>
          </el-table-column>
          <el-table-column label="银行信息" align="left" width="300">
            <template #default="scope">
              <p>银行名称：{{ scope.row.bank_name }}</p>
              <p>银行账号：{{ scope.row.bank_card_number }}</p>
              <p>
                银行国家/城市：{{ scope.row.bank_country }}/{{
                  scope.row.bank_city
                }}
              </p>
              <p>银行地址：{{ scope.row.bank_address }}</p>
              <p>Swift码：{{ scope.row.bank_swift }}</p>
              <p>IBAN码：{{ scope.row.bank_iban }}</p>
              <p>币种：{{ scope.row.currency }}</p>
            </template>
          </el-table-column>
          <el-table-column label="收款信息" align="left" width="250">
            <template #default="scope">
              <p>付款方式：{{ scope.row.pay_way }}</p>
              <p>收款国家：{{ scope.row.receive_country }}</p>
              <p>收款地区：{{ scope.row.receive_city }}</p>
              <p>收款人地址：{{ scope.row.receive_address }}</p>
            </template>
          </el-table-column>
          <el-table-column label="附件信息" align="center" width="200">
            <template #default="scope">
              <el-link
                target="_blank"
                style="margin-right: 10px"
                v-for="item in statusList"
                :type="success"
                :href="item.file_url"
                >{{ item.file_cn_name }}</el-link
              >
            </template>
          </el-table-column>
          <el-table-column
            prop="sys_review_status"
            label="系统审核"
            align="center"
          >
            <template #default="scope">
              <el-tag
                :type="
                  scope.row.sys_review_status == 1
                    ? 'success'
                    : scope.row.sys_review_status == 0
                    ? 'warning'
                    : 'danger'
                "
              >
                {{
                  scope.row.sys_review_status == 1
                    ? "已审核"
                    : scope.row.sys_review_status == 2
                    ? "拒绝"
                    : "未审核"
                }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="createtime"
            label="添加时间"
            :formatter="formatterTime"
            align="center"
            width="90"
          ></el-table-column>
          <el-table-column
            label="操作"
            align="center"
            fixed="right"
            width="150"
          >
            <template #default="scope">
              <el-button
                type="danger"
                plain
                v-if="scope.row.sys_review_status == 0"
                @click="handleOperate(scope.$index, scope.row, 'edit')"
                >编辑</el-button
              >
              <el-button
                type="primary"
                v-if="scope.row.sys_review_status == 0"
                @click="handleOperate(scope.$index, scope.row, 'audit')"
                >审核</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      :dialogVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
      :dialogTitle="dialogTitle"
    />
    <AuditForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      :auditDialogVisible="auditDialogVisible"
      :auditForm="auditForm"
      :btnLoading="btnLoading"
      :dialogTitle="dialogTitle"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatterTime, pickerDefaultTime } from "@/utils/utils";
import {
  merchantKycListApi,
  merchantKycAuditApi,
  merchantKycUpdateApi,
} from "@/api/ExAssistant/merchant";
import DialogForm from "./components/merchantKycDialog.vue";
import AuditForm from "./components/auditForm.vue";
const loading = ref(false);
const tableData = ref([]);
const dialogVisible = ref(false);
const dialogTitle = ref("");
const auditDialogVisible = ref(false);
const btnLoading = ref(false);
const btnType = ref(null);

const formData = {
  merchant_id: "",
  timeData: [],
};
const form = reactive({ ...formData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
// 新增、编辑参数
const dialogFormData = {
  merchant_id: "",
  account_type: "",
  company_name: "",
  company_alias: "",
  company_business: "",
  real_name: "",
  contact: "",
  website: "",
  address: "",
  bank_name: "",
  bank_country: "",
  bank_city: "",
  bank_address: "",
  bank_card_number: "",
  bank_swift: "",
  bank_iban: "",
  currency: "",
};
const dialogForm = reactive({ ...dialogFormData });
const auditFormData = {
  id: "",
  review_status: "",
  review_reason: "",
};
const auditForm = reactive({ ...auditFormData });
//参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    merchant_id: form.merchant_id,
  };
};
// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;
  try {
    const res = await merchantKycListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
//查询
const onQuery = async () => {
  getTbaleData();
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
  getTbaleData();
};
// 新增
const onAdd = async () => {
  dialogVisible.value = true;
  dialogTitle.value = "新增商户资料";
  btnType.value = "add";
  Object.assign(dialogForm, { ...dialogFormData });
};
//关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;
  auditDialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, { ...dialogFormData });
  Object.assign(auditForm, { ...auditFormData });
};
// 取消
const cancel = () => {
  dialogVisible.value = false;
  auditDialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, { ...dialogFormData });
  Object.assign(auditForm, { ...auditFormData });
};
// 行操作
const handleOperate = async (index, row, type) => {
  // 编辑
  if (type === "edit") {
    btnType.value = "edit";
    Object.assign(dialogForm, { ...row });
    dialogVisible.value = true;
    dialogTitle.value = "编辑商户资料";
  }
  // 审核
  if (type === "audit") {
    btnType.value = "audit";
    dialogTitle.value = "审核客户信息";
    Object.assign(auditForm, {
      //...row
      id: row.id,
      review_status: "",
      review_reason: "",
    });
    auditDialogVisible.value = true;
  }
};
// 提交
const submit = async (param) => {
  btnLoading.value = true;
  if (btnType.value == "add") {
    delete param.id;
    const res = await merchantKycUpdateApi(param);
    ElMessage({
      message: res.message,
      type: "success",
    });
    dialogVisible.value = false;
  }
  if (btnType.value == "edit") {
    const res = await merchantKycUpdateApi(param);
    ElMessage({
      message: res.message,
      type: "success",
    });
    dialogVisible.value = false;
  }
  if (btnType.value == "audit") {
    const res = await merchantKycAuditApi(param);
    ElMessage({
      message: res.message,
      type: "success",
    });
    auditDialogVisible.value = false;
  }
  btnLoading.value = false;
  getTbaleData();
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
